<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BackgroundController
 *
 * @author tuancasi
 */
class BackgroundController extends Controller {

    public function actionManager($type = '') {

	/*
	 * add img for all categories except send a wish
	 */
	if (isset($_POST['addimg'])) {

	    if (isset($_FILES['image-insert']['name']) && $_FILES['image-insert']['name']) {
		$ext = strtolower(pathinfo($_FILES["image-insert"]["name"], PATHINFO_EXTENSION));
		$newName = uniqid() . '.' . $ext;
		if (move_uploaded_file($_FILES['image-insert']['tmp_name'], UPLOAD_PATH_BACKGROUND . $newName)) {
		    Background::addBackground($newName, $type);
		}
	    }
	}

	/*
	 * delete img for all categories except send a wish
	 */
	if (isset($_POST['deleteimg'])) {
	    if (isset($_POST['name-img']) && $_POST['name-img']) {
		foreach ($_POST['name-img'] as $nameImg) {
		    //delete old file
		    if ($nameImg && file_exists(UPLOAD_PATH_BACKGROUND . $nameImg)) {
			unlink(UPLOAD_PATH_BACKGROUND . $nameImg);
		    }
		    Background::deleteImg($nameImg);
		}
	    }
	}
	//get list img
	$aImgs = Background::getAllBackground($type);

	$this->render("manager", array('aImgs' => $aImgs));
    }

}

